package edu.gdin.ecommerce.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import edu.gdin.ecommerce.dao.CardDAO;
import edu.gdin.ecommerce.model.Card;
import edu.gdin.ecommerce.service.ICardService;
import edu.gdin.ecommerce.util.DateUtil;
import edu.gdin.ecommerce.util.GenerateRandom;
import edu.gdin.ecommerce.util.Pager;

@Service
public class CardService implements ICardService {

	private CardDAO cardDAO;
	
	public CardDAO getCardDAO() {
		return cardDAO;
	}
	
	@Resource
	public void setCardDAO(CardDAO cardDAO) {
		this.cardDAO = cardDAO;
	}

//	@Override
	public boolean addCard(Card card) {
		try {
			cardDAO.insert(card);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Pager getCardPagerByAccountId(Integer id, Integer cpg){
		return this.cardDAO.getCardPagerByAccountId(id, cpg);
	}

	public Pager getCardRecoredAccountId(Integer id, Integer cpg){
		return this.cardDAO.getCardRecoredByAccountId(id, cpg);
	}

//	@Override
	public boolean resetCardPasswd(Integer cardId,String passwd) {
		try {
			Card card = (Card) cardDAO.findByPk(Card.class, cardId);
			passwd = (passwd ==null || passwd.trim().equals("")) ?  GenerateRandom.randomStringNum(6) : passwd;
			card.setPassword(passwd);
			card.setUpdateTime(DateUtil.now());
//			cardDAO.update(card);
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

//	@Override
	public boolean updateStatus(Integer cardId, Integer status) {
		try {
			Card card = (Card) cardDAO.findByPk(Card.class, cardId);
			card.setStatus(status);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
